package ch.usi.inf.pf2.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.usi.inf.pf2.cell.Converter;

/**
 * 
 * @author Lorenzo Baracchi <lorenzo.baracchi@usi.ch>
 *
 */
public class CellRangeWindow
{
    private JFrame cellFrame;
    private JPanel panel;
    private JSpreadSheetGUI spreadTable;

    public CellRangeWindow (JSpreadSheetGUI spreadTable)
    {
	   this.spreadTable=spreadTable;
	   cellFrame= new JFrame("Insert Constant in the cell");
	   makeFrame();
    }
    
    public void makeFrame ()
    {
	   cellFrame.setLocationRelativeTo(spreadTable.getFrame());
	   cellFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	   cellFrame.getContentPane().setLayout(new BorderLayout());
	   panel = new JPanel();
	   panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
	   makeContent();
	   cellFrame.getContentPane().add(panel, BorderLayout.CENTER);
	   cellFrame.pack();
	   cellFrame.setVisible(true);
    }
    
    public void makeContent ()
    {
	   JLabel select = new JLabel("Select the range:");
	   panel.add(select);
	   
	   JPanel inner = new JPanel(new GridLayout(4, 3, 5, 5));
	   panel.add(inner);
	   
	   inner.add(new JLabel());
	   JLabel col = new JLabel("Column:");
	   inner.add(col);
	   JLabel row = new JLabel("Row:");
	   inner.add(row);
	   
	   JLabel start = new JLabel("Start cell");
	   inner.add(start);
	   final JComboBox col1 = new JComboBox(colIndexes());
	   col1.setSelectedIndex(0);
	   inner.add(col1);
	   final JComboBox row1 = new JComboBox(rowIndexes());
	   row1.setSelectedIndex(0);
	   inner.add(row1);
	   
	   JLabel end = new JLabel("End cell");
	   inner.add(end);
	   final JComboBox col2 = new JComboBox(colIndexes());
	   col2.setSelectedIndex(0);
	   inner.add(col2);
	   final JComboBox row2 = new JComboBox(rowIndexes());
	   row2.setSelectedIndex(0);
	   inner.add(row2);
	   
	   inner.add(new JLabel());
	   final JButton ok = new JButton("Insert");
	   ok.addActionListener(new ActionListener()
	   {
	       @Override
	       public void actionPerformed(ActionEvent arg0)
	       {
	   	   	String s= (String)col1.getSelectedItem()+(String)row1.getSelectedItem()+
	   	   		":"+(String)col2.getSelectedItem()+(String)row2.getSelectedItem();
	   	   	String actualText = spreadTable.getTextField().getText();
	   	   	int cursorPosition = spreadTable.getTextField().getCaretPosition();
	   	   	String tmp = actualText.substring(cursorPosition, actualText.length());
	   	   	actualText=actualText.substring(0, cursorPosition);
	   	   	s=actualText+s+tmp;
	   	   	spreadTable.getTextField().setText(s);
        	   	cellFrame.dispose();
	       }
	   });
	   inner.add(ok);
	   final JButton cancel = new JButton("Cancel");
	   cancel.addActionListener(new ActionListener()
	   { 
	       @Override
	       public void actionPerformed(ActionEvent e)
	       {
	   	   	cellFrame.dispose();
	       }
	   });
	   inner.add(cancel);
    }
    
    public String[] rowIndexes ()
    {
	   String[] n = new String[30];
	   for(int i=1; i<n.length; i++)
		  n[i]=i+"";
	   return n;
    }
    
    public String[] colIndexes ()
    {
	   String[] n = new String[30];
	   for(int i=1; i<n.length; i++)
	   {
		  Converter c = new Converter();
		  c.setCellReferenceNumbers(new int[] {i,1});
		  c.convertToString();
		  n[i]=c.getCellLetters();
	   }
	   return n;
    }
}
